;
(function($) {
    function spinner(element, options) {
        var _self = this;
        this.options = $.extend({}, spinner.options, options);
        this.wrapEle = $('<span></span>').addClass(this.options.style);
        this.enterEle = element.addClass('spinner-value');
        this.countEle = $('<span class="spinner-count"></span>');
        this.addEle = $('<b class="spinner-add">+</b>');
        this.cutEle = $('<b class="spinner-cut">-</b>');
        this.enterEle.after(this.wrapEle);
        this.wrapEle.append(this.enterEle);
        this.wrapEle.append(this.countEle);
        this.countEle.append(this.addEle);
        this.countEle.append(this.cutEle);
        this._timer = null;
        this.disabled(this.options.disabled);
        spinner.value.call(this, this.options.value || this.enterEle.val());
        this.maxValue(this.options.maxValue);
        this.minValue(this.options.minValue);
        this.increment(this.options.increment);
        this.places(this.options.places);
        this.enterEle.bind('blur', function() {
            _self.value($(this).val());
            _self.wrapEle.removeClass('think-spinner-focus');
        });
        this.enterEle.bind('focus', function() {
            _self.wrapEle.addClass('think-spinner-focus');
        });
        this.addEle.bind('mousedown', function() {
            spinner.timer(function(timer) {
                _self._timer = timer;
                _self.stackAdd();
            });
        });
        this.cutEle.bind('mousedown', function() {
            spinner.timer(function(timer) {
                _self._timer = timer;
                _self.stackCut();
            });
        });
        this.addEle.add(this.cutEle).bind('mouseup', function() {
            clearTimeout(_self._timer);
        });
    }
    //当前值叠加
    spinner.prototype.stackAdd = function() {
        this.value(Number(this._currValue) + this._increment);
        return this;
    }
    //当前值叠减
    spinner.prototype.stackCut = function() {
        this.value(Number(this._currValue) - this._increment);
        return this;
    }
    //设置或返回当前值
    spinner.prototype.value = function(value) {
        if (arguments.length) {
            spinner.value.call(this, spinner.findValue.call(this, value));
            $.isFunction(this.options.onChange) && this.options.onChange.call(this);
        } else {
            return this._currValue;
        }
        return this;
    }
    //设置或返回最大值
    spinner.prototype.maxValue = function(value) {
        if (arguments.length) {
            if (!$.isNumeric(value)) {
                value = null;
            }
            this._maxValue = value;
        } else {
            return this._maxValue;
        }
        return this;
    }
    //设置或返回最小值
    spinner.prototype.minValue = function(value) {
        if (arguments.length) {
            if (!$.isNumeric(value)) {
                value = null;
            }
            this._minValue = value;
        } else {
            return this._minValue;
        }
        return this;
    }
    //设置或返回增量值
    spinner.prototype.increment = function(number) {
        if (arguments.length) {
            if ($.isNumeric(number)) {
                this._increment = Number(number);
            } else {
                this._increment = this._increment ? this._increment : 1;
            }
        } else {
            return this._increment;
        }
        return this;
    }
    //设置或返回保留小数点位数
    spinner.prototype.places = function(number) {
        if (arguments.length) {
            if ($.isNumeric(number)) {
                this._places = parseInt(number);
            } else {
                this._places = this._places ? this._places : 0;
            }
        } else {
            return this._places;
        }
        return this;
    }
    //禁用或启动或获取控件状态
    spinner.prototype.disabled = function(status) {
        if (arguments.length) {
            this._disabled = status ? true : false;
            if (this._disabled) {
                this.wrapEle.addClass('think-spinner-disabled');
            } else {
                this.wrapEle.removeClass('think-spinner-disabled');
            }
            this.enterEle.attr('disabled', this._disabled);
        } else {
            return this._disabled;
        }
        return this;
    }
    //速度加载器
    spinner.timer = function(callback) {
        var _timer, _speed = 280;
        var _func = function() {
            (_speed -= 40) < 40 && (_speed = 40);
            _timer = setTimeout(_func, _speed);
            $.isFunction(callback) && callback(_timer);
        }
        _func();
        return _timer;
    }
    var i = 0;
    //设置当前值
    spinner.value = function(value) {
        if (!this._disabled) {
            var _value = value;
            if ($.isNumeric(value)) {
                if ($.isNumeric(this._maxValue) && _value > this._maxValue) {
                    _value = this._maxValue;
                } else if ($.isNumeric(this._minValue) && this._minValue > _value) {
                    _value = this._minValue;
                }
                _value = Number(_value).toFixed(this._places);
            } else {
                _value = '';
            }
            this._currValue = _value;
            this.enterEle.val(this._currValue);
            return this;
        }
    }
    //检测数据
    spinner.findValue = function(value) {
        if (value && $.isNumeric(value)) {
            var _pattern = new RegExp('^([0-9]+)' + (this._places > 0 ? '?(.[0-9]{0,' + this._places + '})' : '$'));
            var _result = value.toString().match(_pattern);
            if ($.isArray(_result)) {
                value = RegExp.$1 + RegExp.$2;
            } else {
                value = this._currValue;
            }
        } else {
            if (value != '') {
                value = this._currValue ? this._currValue : '';
            }
        }
        return value;
    }
    spinner.options = {
        style: 'think-spinner',
        value: null, //起始值
        maxValue: null, //最大值
        minValue: 0, //最小值
        increment: 1, //增量值
        places: 0, //保留的小数点位数
        disabled: false, //是否禁用
        mousewheel: true, //是否滚动滚轮改变值
        onChange: null, //是发生改变时触发
        onCreate: null //插件初始化后触发
    }
    $.fn.spinner = function(options) {
        return this.each(function() {
            $(this).data('thinkSpinner', new spinner($(this), options));
        });
    }
    $.fn.getSpinner = function() {
        return $(this).data('thinkSpinner');
    }
})(jQuery);
